var themeObj = {
    arr: [],
    obj: [],
    cid: 0,
};
pageParam = getParam();
themeObj.cid = pageParam['cid'];
$('.theme input').val(pageParam['name']);


$(function () {
    var opt = {};
    opt.datetime = {
        preset: 'date'
    };
    var currYear = (new Date()).getFullYear();
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式 
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        showNow: true,
        nowText: "今天",
        startYear: currYear, //开始年份
        endYear: currYear + 1, //结束年份
    };
    $("#activeTime").mobiscroll($.extend(opt['datetime'], opt['default'])); //显示日期
})
judgeLogin()
getTheme();
// 获取主题列表
function getTheme() {
    _ajax('home/dymanic/category', function (ret, err) {
        if (ret && ret.code == 200 && ret.result && ret.result.length > 0) {
            themeObj.obj = ret.result;
            for (var i = 0, len = ret.result.length; i < len; i++) {
                themeObj.arr.push(ret.result[i].name);
            }
        }
    })
}

// 切换城市
function _areaSubmit(sCity) {
    $('.city input').val(sCity);
}

// 弹框选择
function selectAction(type) {
    var buttons = type == 'theme' ? themeObj.arr : ['应约者买单', 'AA制', '发起者买单'];
    _action('', buttons, function (index) {
        if (index != (buttons.length + 1)) {
            $('.' + type + ' input').val(buttons[index - 1]);
            if (type == 'theme') {
                themeObj.cid = themeObj.obj[index - 1].id;
            }
        }
    })
}

// 格式化输入
function num(obj) {
    obj.value = obj.value.replace(/[^0-9.]*$/, ""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
}
var imgArr = [];
// 获取图片
function selectFile() {
    $('#pic').click();
}
$('#pic').on('change', function () {
    if (!this.files[0]) {
        return;
    }
    imgArr.push(this.files[0]);
    var reader = new FileReader();
    reader.readAsDataURL(this.files[0]);

    reader.onload = function () {
        // _log(reader.result)
        imgCss(reader.result, function (w, h) {
            if (w < h) {
                var html = `
                <li class="i-list">
                    <div class="img img-w">
                        <img src="${reader.result}" alt="">
                    </div>
                    <i class="aui-iconfont aui-icon-close" onclick="delImg(this)"></i>
                </li>`;
            } else {
                var html = `
                <li class="i-list">
                    <div class="img img-h">
                        <img src="${reader.result}" alt="">
                    </div>
                    <i class="aui-iconfont aui-icon-close" onclick="delImg(this)"></i>
                </li>`;
            }
            $('#pic').before(html);
        })
    }
})

// 删除
function delImg(obj) {
    var index = $(obj).parent().index();
    $('.img-wrap .i-list').eq(index).remove();
    imgArr.splice(index, 1);
}

// 发布
function submit() {
    var theme = themeObj.cid;
    var content = $.trim($('.dy-content input').val());
    var time = $('.time input').val();
    var city = $('.city input').val();
    var pay = $('.pay input').val();
    var budget = $('.budget input').val();
    var type = pageParam['type'] || 2;

    if (!theme) {
        _msg('请选择约会主题');
        return;
    }
    if (!content) {
        _msg('请填写约会内容');
        return;
    }
    if (!time) {
        _msg('请选择约会时间');
        return;
    }
    if (!city) {
        _msg('请选择约会城市');
        return;
    }
    if (!pay) {
        _msg('请选择买单方式');
        return;
    }
    if (!budget) {
        _msg('请填写约会预算');
        return;
    }
    _loading()
    if(imgArr.length > 0){
        _upImgs(imgArr, function(arr){
            _ajax('home/Dymanic/adddymanic', function (ret, err) {
                _log(JSON.stringify(ret))
                _log(JSON.stringify(err))
                _loadingClose();
                _msg(ret.msg);
                if (ret && ret.code == 200) {
                    timerWin();
                }
            }, {
                datetime: time,
                citys: city,
                content:content,
                userid: myuserid,
                call: pay,
                filter: budget,
                type: type,
                category_id: theme,
                masterimg: arr
            })
        })
    }else{
        _ajax('home/Dymanic/adddymanic', function (ret, err) {
            _log(JSON.stringify(ret))
            _log(JSON.stringify(err))
            _loadingClose();
            _msg(ret.msg);
            if (ret && ret.code == 200) {
                timerWin();
            }
        }, {
            datetime: time,
            citys: city,
            content:content,
            userid: myuserid,
            call: pay,
            filter: budget,
            type: type,
            category_id: theme,
        })
    }
}

var cityLen = 1; // 1 当前城市 4常驻城市
function _changeCity(city) {
    closeModalFrm();
    _areaSubmit(city);
}
// 修改当前城市
function _showCity() {
    $('#modal-wrap').removeClass('new-hide');
    _load('#modal-wrap', 'frame0/city');
}
// 隐藏模态框
function closeModalFrm() {
    $('#modal-wrap').addClass('new-hide');
}